import datetime
import re
import time
import json
from django.views import View
from django.http import HttpResponse, JsonResponse
from Applications.ClassManager.AutoLogin.ClassJsonGet import AutoRequests
from Applications.ClassManager.AutoLogin.SeleniumJsonGet import AutoSelenium
from Applications.ClassManager.ClassData.Data import ClassMemberInfo
from Applications.ClassManager.Course.mongoModel import louisMongo

# Create your views here.



class DXXJsonView(View):
    def get(self,request):
        useSelenium = True
        url = 'https://jxtw.h5yunban.cn/jxtw-qndxx/cgi-bin/branch-api/course/records?pageSize=20&pageNum=1&desc=createTime&nid=N001300081008&course=C0002&accessToken=1BCB4EB0-E48F-4AE8-8853-A9A46FE73F0F'
        if not useSelenium:
            jsonStr = AutoRequests(url)
            jsonDict = json.loads(jsonStr)
            result = jsonDict.get("result")
            list = result.get("list")
            jsonList = []
            for member in list:
                name = member.get("cardNo")
                jsonList.append(name)
        else:
            auto = AutoSelenium()
            jsonList = auto.Selenium("180851_102377", "466976")
        ClassMemberInfo_Reverse = {}
        for key, value in ClassMemberInfo.items():
            ClassMemberInfo_Reverse[value] = key
        MemberDict = {}
        MemberList = []
        ClassMemberInfo_Do = {}
        for Mem in ClassMemberInfo:
            ClassMemberInfo_Do[Mem] = ClassMemberInfo.get(Mem)
        for member in jsonList:
            name = member
            index = name

            if "-" in name:
                index = name[0:8]
                name = ClassMemberInfo.get(index)
            elif len(name) == 2 and re.search(r'[1-9]', name[0]):
                index = '180851' + name
                name = ClassMemberInfo.get(index)
            elif len(name) == 8:
                index = name
                name = ClassMemberInfo.get(index)
            elif len(name) < 4:
                index = ClassMemberInfo_Reverse.get(name)
                name = name
            elif len(name) > 8:
                if re.search(r'[1-9]', name[0]):
                    index = name[0:8]
                    name = ClassMemberInfo.get(index)
                else:
                    if re.search(r'[1-9]', name[2]):
                        name = name[0:2]
                        index = ClassMemberInfo_Reverse.get(name)
                    else:
                        name = name[0:3]
                        index = ClassMemberInfo_Reverse.get(name)
            else:
                if re.search(r'[1-9]', name[0]):
                    index = name[0:8]
                    name = ClassMemberInfo.get(index)
                else:
                    name = name[0:3]
                    index = ClassMemberInfo_Reverse.get(name)

            try:
                ClassMemberInfo_Do.pop(index)
            except:
                print(index,name)

            MemberDict["index"] = index
            MemberDict["name"] = name

            MemberList.append(eval(str(MemberDict)))

        NotDict = {}
        NotList = []
        for Mem in ClassMemberInfo_Do:
            NotDict["index"] = Mem
            NotDict["name"] = ClassMemberInfo_Do.get(Mem)
            NotList.append(eval(str(NotDict)))

        ReturnJson = {}
        ReturnJson["DoMember"] = MemberList
        ReturnJson["NotMember"] = NotList
        ReturnJson["AllNum"] = len(ClassMemberInfo)
        ReturnJson["DoNum"] = len(MemberList)
        ReturnJson["DontNum"] = len(NotList)
        ReturnJson["Time"] = time.strftime("%Y-%m-%d", time.localtime())


        jsonStr = json.dumps(ReturnJson)
        return HttpResponse(jsonStr)

    def post(self,request):
        pass



class CourseJsonView(View):
    def get(self, request, Name='Louis'):
        Week = request.GET.get("week")
        if Week == None:
            yearWeek = datetime.datetime.now().isocalendar()[1]
            Week = yearWeek - 35
            print(Week)
        mongo = louisMongo(init=False)
        mongo.DB('Utils')
        mongo.Col('LouisCourse-2020-9')
        mongo = mongo.Get()

        course = ''

        for item in mongo.find():
            if item.get("week") == str(Week):
                course = item.get("data")

        course = "[" + course[1:-1] + "]"
        course = course.replace("'", '"')
        course = json.loads(course)
        # weekInfo = {"week": Week}
        # course.append(weekInfo)
        print(type(course))
        return JsonResponse(course, safe=False)